import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "@/views/login/index.vue"
import Layout from "@/views/layout"
import Home from "@/views/layout/components/Home"
import Article from "@/views/article"
import Publish from "@/views/publish"
import Images from "@/views/images"
import Comment from "@/views/comment"
import Settings from "@/views/settings"
import Fans from "@/views/fans"

Vue.use(VueRouter)

const routes = [
  {
    path:'/login',
    component:Login
  },

  {
    path:"/",
    component:Layout,
    children:[
      {
        path:'/',
        component:Home
      },
      {
        path:'/article',
        component:Article
      },
      {
        path:'/publish',
        component:Publish
      },
      {
        path:'/images',
        component:Images
      },
      {
        path:'/comment',
        component:Comment
      },
      {
        path:'/settings',
        component:Settings
      },
      {
        path:'/fans',
        component:Fans
      },
    ]
  }
]

const router = new VueRouter({
  routes,
  mode:'history'
  
})
router.beforeEach((to, from, next) => {
  const user = JSON.parse(window.sessionStorage.getItem('user'))
    if(to.path!=='/login'){
      if(user){
        next()
      }
      else{
        next('/login')
      }
    }
    else{
      next()
    }
})

export default router
